#include "stdafx.h"
#include "ckMatrixB.h"

namespace ck
{
	namespace data
	{
		MatrixB::MatrixB()
		{
			m_data = NULL;
		}

		MatrixB::~MatrixB()
		{
			if (m_data)
			{
				delete[] m_data;
				m_data = NULL;
			}
		}

		RESULTCODE MatrixB::Create(z_byte* pBytes, z_uint32 w, z_uint32 h)
		{
			if (m_data)
			{
				delete[] m_data;
				m_data = NULL;
			}

			z_uint64 nSize = w * h;
			m_data = new z_byte[nSize];
			memcpy(m_data, pBytes, nSize * sizeof(z_int32));
			return RC_SUCCESS;
		}

		z_byte* MatrixB::GetData()
		{
			return m_data;
		}

		z_byte* MatrixB::GetDataPtr(z_uint32 i, z_uint32 j)
		{
			return (m_data + i * m_width + j);
		}

		z_byte MatrixB::GetValue(z_uint32 i, z_uint32 j)
		{
			return *(m_data + i * m_width + j);
		}

		void MatrixB::operator/=(double n)
		{
			if (n == 0.0)
			{
				return;
			}
			if (m_data)
			{
				z_byte* ptr = m_data;
				for (z_uint32 i = 0; i < m_height; i++)
				{
					for (z_uint32 j = 0; j < m_width; j++)
					{
						*ptr = *ptr / n;
					}
				}
			}
		}
	}
}
